//
//  ICChnMessageCell.m
//  ICome
//
//  Created by Administrator on 2016/11/4.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICChnMessageCell.h"
#import "ICChnModel.h"

@interface ICChnMessageCell()

@property (nonatomic, strong) UILabel *timeLabel;

@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UILabel *titleL;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UIView * contentV;
@property (nonatomic, strong) UIImageView *unreadPoint;


@end

@implementation ICChnMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = IColor(229, 229, 229);
        self.titleL.font               = [UIFont systemFontOfSize:17];
        self.timeLabel.font            = [UIFont systemFontOfSize:14];
        self.contentLabel.font         = [UIFont systemFontOfSize:15];
        
        
        [self.contentView addSubview:self.timeLabel];
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.unreadPoint];
        [self.bgView addSubview:self.titleL];
        
        [self.bgView addSubview:self.contentLabel];
        [self.bgView addSubview:self.lineView];
        [self.bgView addSubview:self.contentV];
        
        [self setConstraint];
    }
    return self;
}

#pragma mark - Constraint
- (void)setConstraint
{
    NSLog(@"super setConstraint");
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(5);
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.width.mas_equalTo(250);
    }];
    
    [self.unreadPoint mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(2);
        make.top.equalTo(self.timeLabel.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(8, 8));
    }];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.top.equalTo(self.timeLabel.mas_bottom).offset(5);
        make.bottom.equalTo(self.mas_bottom).offset(-5);
        make.width.mas_equalTo(self.width-30);
    }];
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.top.equalTo(self.bgView.mas_top).offset(16);
        make.height.mas_equalTo(23);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.top.equalTo(self.bgView.mas_top).offset(43);
        make.height.mas_equalTo(1);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.top.equalTo(self.lineView.mas_top).offset(5);//12
    }];
    
    [self.contentV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(5);
        make.width.mas_equalTo(App_Frame_Width-60);
        make.height.mas_equalTo(11);
    }];
    
}

- (void)setMessage:(ICChnModel *)message
{
    _message = message;
    if(_message){
        
        // 时间处理
        if (_message.hideTime) { // 隐藏时间
            
            [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
            self.timeLabel.hidden = YES;
        }else{
            self.timeLabel.text = message.time;
            self.timeLabel.hidden = NO;
             [self.timeLabel sizeToFit];
            [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(self.timeLabel.frame.size.width);
                make.height.mas_equalTo(21);
            }];
        }
        
        if(_message.title.length > 0){
            self.titleL.hidden = NO;
            [self.titleL mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(23);
            }];
        self.titleL.text = message.title;//[message.dictkey objectForKey:@"title"];
        }else{
            self.titleL.hidden = YES;
            [self.titleL mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
        }
        
        if(_message.subject.length > 0){
            self.contentLabel.hidden = NO;
            self.contentLabel.text = message.subject; //[message.dictkey objectForKey:@"subject"];
            [self.contentLabel sizeToFit];
            // 强制更新
            [self layoutIfNeeded];
            
            [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(self.contentLabel.height);
            }];
        }else{
            self.contentLabel.hidden = YES;
            [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
        }
        
      
        if (_message.status == ICMessageStatus_unRead) {
            self.unreadPoint.hidden = NO;
        } else {
            self.unreadPoint.hidden = YES;
        }
        
        // 强制更新
        [self layoutIfNeeded];
        [self.contentV removeAllSubviews];
        CGFloat messAgeH = 0;
        for(NSInteger i = 0;i < message.content.count;i++){
            NSDictionary * dict = message.content[i];
            CGFloat Hight = [self calculateheight:dict[@"value"]];
            [self addView:dict  count:i  height:Hight topheight:messAgeH];
            if(Hight > 22){
                messAgeH += (Hight+7);
            }else{
                messAgeH += 22;
            }
        }
        
        [self.contentV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(messAgeH + 11);
        }];
        
        // 强制更新
        [self layoutIfNeeded];
        CGFloat timeLabelH = _message.hideTime? 0:21;
        self.message.cellHeight = self.contentV.y + self.contentV.height + 15 + timeLabelH;
         NSLog(@"hideTime %@",_message.hideTime? @"Y":@"N");
        NSLog(@"cellHeight = %f,%f,%F",self.message.cellHeight,self.timeLabel.height,self.contentV.height);
    }
    
}

-(void)addView:(NSDictionary *)dict  count:(NSUInteger)count  height:(CGFloat)height topheight:(CGFloat)topheight
{
    UILabel * leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, topheight, 65,15)];
    leftLabel.textColor = ICRGB(0x999999);
    leftLabel.font = [UIFont systemFontOfSize:12];
    leftLabel.textAlignment = NSTextAlignmentRight;
    NSString * str = [dict objectForKey:@"key"];
    leftLabel.text = [NSString stringWithFormat:@"%@：",str.length > 4 ?[str substringFromIndex:str.length-4]:str];
    
    UILabel * rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(leftLabel.width,topheight,App_Frame_Width-125,height)];
     NSLog(@"rightLabel.frame.x = ,%F,%f",leftLabel.width,rightLabel.frame.origin.x);
    rightLabel.textColor = ICRGB(0x333333);
    rightLabel.font = [UIFont systemFontOfSize:12];
    rightLabel.numberOfLines = 0;
    rightLabel.textAlignment = NSTextAlignmentLeft;
    rightLabel.text = [dict objectForKey:@"value"];
    NSLog(@"rightLabel.frame = %f",rightLabel.frame.origin.x);
    
    [self.contentV addSubview:leftLabel];
    [self.contentV addSubview:rightLabel];
    
}


-(CGFloat)calculateheight:(NSString *)str
{
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width-125, 16)];
    label.font = [UIFont systemFontOfSize:12];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentLeft;
    label.text = str;
    [label sizeToFit];
    return label.frame.size.height;
}





#pragma mark - Getter


- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.backgroundColor = IColor(197, 197, 197);
        _timeLabel.textColor       = [UIColor whiteColor];
        _timeLabel.textAlignment   = NSTextAlignmentCenter;
        _timeLabel.layer.masksToBounds  = YES;
        _timeLabel.layer.cornerRadius   = 5.0;
    }
    return _timeLabel;
}

- (UIImageView *)bgView
{
    if (!_bgView) {
       _bgView         = [[UIImageView alloc] init];
        UIImage *image = [UIImage imageNamedFromiComeSDKBundle:@"icon_chat_spirit_bg"];
        _bgView.image = [image stretchableImageWithLeftCapWidth:0 topCapHeight:image.size.height * 0.5];
        _bgView.layer.cornerRadius  = 5;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}


- (UILabel *)titleL
{
    if (!_titleL) {
        _titleL        = [[UILabel alloc] init];
        _titleL.textColor = ICRGB(0xDE8E32);
    }
    return _titleL;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel        = [[UILabel alloc] init];
        _contentLabel.numberOfLines = 0;
        _contentLabel.textColor = ICRGB(0x000000);
    }
    return _contentLabel;
}


-(UIView *)lineView
{
    if(!_lineView)
    {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = ICRGB(0xdfdfdf);
    }
    return _lineView;
}

-(UIView *)contentV
{
    if(!_contentV)
    {
        _contentV = [[UIView alloc] init];
        _contentV.backgroundColor = [UIColor clearColor];
    }
    return _contentV;
}

-(UIImageView *)unreadPoint {
    if (!_unreadPoint) {
        _unreadPoint = [[UIImageView alloc] init];
        _unreadPoint.image = [UIImage imageNamedFromiComeSDKBundle:@"icon_email_blue"];
        [self.contentView addSubview:_unreadPoint];
    }
    return _unreadPoint;
}


@end
